//
//  UIViewController+XHExtension.m
//  XHFoundationModule
//
//  Created by xinph on 2020/3/16.
//  Copyright © 2020 xinph. All rights reserved.
//

#import "UIViewController+XHExtension.h"

@implementation UIViewController (XHExtension)


- (UIViewController *)currentViewController
{
    UIViewController *currentViewController = [self topMostController];
    
    while ([currentViewController isKindOfClass:[UINavigationController class]] || [currentViewController isKindOfClass:[UITabBarController class]])
    {
        if ([currentViewController isKindOfClass:[UINavigationController class]] && [(UINavigationController *)currentViewController topViewController])
        {
            currentViewController = [(UINavigationController *)currentViewController topViewController];
        }
        else if ([currentViewController isKindOfClass:[UITabBarController class]] && [(UITabBarController *)currentViewController selectedViewController])
        {
            currentViewController = [(UITabBarController *)currentViewController selectedViewController];
        }
    }
    
    currentViewController = [currentViewController topMostController];
    
    return currentViewController;
}

- (UIViewController *)topMostController
{
    UIViewController *topController = self;
    while ([topController presentedViewController])
        
        topController = [topController presentedViewController];
    
    return topController;
}

@end
